package com.xxl.sso.core.web;

import com.xxl.sso.core.conf.Conf;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/sso")
public class SsoReceiveController {
    /**
     * 调用此方法过滤器会拦截处理参数中的sessionId
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @GetMapping("/receive")
    public void receive(HttpServletRequest request, HttpServletResponse response) throws IOException {
        StringBuilder builder = new StringBuilder();
        // 作为baseUrl
        String toUrl = request.getParameter(Conf.TO_URL);
        builder.append(toUrl);
        for (Map.Entry entry : request.getParameterMap().entrySet()) {
            String key = entry.getKey().toString();
            // 排除这两个参数
            if ((!Conf.SSO_SESSIONID.equals(key)) && (!Conf.TO_URL.equals(key))) {
                builder.append("&").append(key).append("=").append(request.getParameter(key));
            }
        }
        response.sendRedirect(builder.toString());
    }
}
